using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using NUnit.Framework;
using System.IO;
using System.Collections.ObjectModel;
using System.Xml;
using Microsoft.Health.Samples.PatientConnect.ContosoHealth.HealthVaultAdapter;

namespace HVAdapterUnitTestCases
{
    [TestFixture]
    public class InboundTestCases
    {

        [Test, Description("Gets all the patient for the provider within time span till now")]
        public void TestGetPatientByDate()
        {
            ReadHealthVaultItemTypes inbound = new ReadHealthVaultItemTypes(UnitTestConstants.ApplicationId);
            Patient patient = new Patient(UnitTestConstants.PatientXmlFilePath);
            patient.RegisteredWithHealthVault = false;
            //Updating the patients.xml file for "RegisteredWithHealthVault=false" 
            patient.UpdatePatient("RegisteredWithHealthVault=true");
            //Selecting patients who registered after the specified Date time and updating "RegisteredWithHealthVault=ture" in patients.xml file
            inbound.GetPatients(UnitTestConstants.PatientXmlFilePath, Convert.ToDateTime("06/03/2008"));
            //Reading the xml file for verification of "RegisteredWithHealthVault=ture"
            ReadPatient();
        }

        [Test, Description("Gets patients medication records")]
        public void TestGetpatientMedication()
        {
            ReadHealthVaultItemTypes inbound = new ReadHealthVaultItemTypes(UnitTestConstants.ApplicationId);
            object o = UnitTestHelperUtility.RunInstanceMethod(typeof(ReadHealthVaultItemTypes), "GetPatientMedication",
                inbound, new object[2] { UnitTestConstants.OfflinePersonId,  UnitTestConstants.RecordId });
            Collection<XmlDocument> medicationCollection = (Collection<XmlDocument>)o;
            Assert.IsNotEmpty(medicationCollection, "Medication Collection is empty");
        }

        [Test, Description("Gets the patients basic demographic information")]
        public void TestGetBasicInformation()
        {
            ReadHealthVaultItemTypes inbound = new ReadHealthVaultItemTypes(UnitTestConstants.ApplicationId);
            UnitTestHelperUtility.RunInstanceMethod(typeof(ReadHealthVaultItemTypes), "GetBasicInformation",
                inbound, new object[4] {new Guid("9d9d47a5-83ad-4248-ac24-ee6724fd28ab"),
                    UnitTestConstants.OfflinePersonId,
                    UnitTestConstants.RecordId,
                UnitTestConstants.PatientXmlFilePath });
        }

        [Test, Description("Gets patients image")]
        public void TestGetPersonalImage()
        {
            ReadHealthVaultItemTypes inbound = new ReadHealthVaultItemTypes(UnitTestConstants.ApplicationId);

            if (File.Exists(Path.Combine(UnitTestConstants.ContentFolderpath  ,UnitTestConstants.ImageFileName)))
                File.Delete(Path.Combine(UnitTestConstants.ContentFolderpath ,UnitTestConstants.ImageFileName));

            UnitTestHelperUtility.RunInstanceMethod(typeof(ReadHealthVaultItemTypes), "GetPersonalImage",
                inbound, new object[3] {UnitTestConstants.OfflinePersonId,
                    UnitTestConstants.RecordId,
                UnitTestConstants.ContentFolderpath });

            Assert.IsTrue(File.Exists(Path.Combine(UnitTestConstants.ContentFolderpath , UnitTestConstants.ImageFileName)),
                "Image file not downloaded");
        }

        /// <summary>
        /// Reads the patients from patients.xml file and Asserts on the basis of person id and Registered with Health vault
        /// </summary>
        private void ReadPatient()
        {
            Patient patient = new Patient(UnitTestConstants.PatientXmlFilePath);
            List<Patient> patientCollection = patient.SelectPatientRecords("");
            for (int i = 0; i < patientCollection.Count; i++)
            {
                Assert.AreNotEqual(patientCollection[i].PersonId, Guid.Empty, "Empty Guid found i.e. no patient fetched");
                Assert.AreNotEqual(patientCollection[i].RegisteredWithHealthVault, "true", "Empty Guid found i.e. no patient fetched");
            }
        }

        /// <summary>
        /// Saves identity code which are generated in testing to "TestIdCodes.txt" file
        /// </summary>
        /// <param name="Guid"></param>
        /// <param name="IdCode"></param>
        private void SaveTestIdentityCode(string Guid, string IdCode)
        {
            StringBuilder Idcodes = new StringBuilder();
            Idcodes.Append(Guid);
            Idcodes.Append("       ");
            Idcodes.Append(IdCode);
            Idcodes.Append("       ");
            Idcodes.AppendLine(DateTime.Now.ToString());
            File.AppendAllText(UnitTestConstants.TestPatientIdFilePath, Idcodes.ToString());
        }

        /// <summary>
        /// Saves the Child id which are generated in testing to "ChildApps.txt" file
        /// </summary>
        /// <param name="Guid"></param>
        private void SaveGeneratedChildAppID(string Guid)
        {
            StringBuilder Idcodes = new StringBuilder();
            Idcodes.Append(Guid);
            Idcodes.Append("        ");
            Idcodes.AppendLine(DateTime.Now.ToString());
            File.AppendAllText(UnitTestConstants.TestChildAppIdFilePath, Idcodes.ToString());
        }
        /// <summary>
        /// Creates the xml file after generating the patient
        /// </summary>
        private void CreatePatientsXml()
        {
            XmlTextWriter xmlwriter = new XmlTextWriter(UnitTestConstants.PatientXmlFilePath, Encoding.UTF8);
            xmlwriter.WriteStartDocument();
            xmlwriter.WriteStartElement("Patients");
            xmlwriter.WriteEndElement();
            xmlwriter.Close();
            Patient patient = new Patient(UnitTestConstants.PatientXmlFilePath);
            patient.PatientId = Guid.NewGuid();
            patient.AddPatient();
        }
    }
}
